<?php

use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Http\Request;


class UsersController extends BaseController{
	public function index()
	{
		$users = User::paginate(10);
		
		return View::make('user/index', compact('users'));
	}
	
	public function add()
	{
		return View::make('user/add');
	}
	
	public function edit(User $user)
	{		
		return View::make('user/edit', compact('user'));
	}
	
	public function delete(User $user)
	{
		return View::make('user/delete', compact('user'));
	}
	
	public function handleAdd()
	{
		$user = new User();
		
		$user->name = Input::get('name');
		$user->email = Input::get('email');
		$user->password = Hash::make(Input::get('password'));
		$user->type = Input::get('type');
		
		$user->save();
						
		return Redirect::action('UsersController@index');
	}
	
	public function handleEdit()
	{
		$user = User::findOrFail(Input::get('id'));
		
		$user->name = Input::get('name');
		$user->email = Input::get('email');
		$user->password = Input::get('password');
		$user->type = Input::get('type');
		
		$user->save();
			
		return Redirect::action('UsersController@index');
	}
	
	public function handleDelete()
	{
		$user = User::findOrFail(Input::get('user'));
				
		$user->delete();
			
		return Redirect::action('UsersController@index');
	}
	
	public function login()
	{
		return View::make('user/login');
	}
	
	public function handleLogin()
	{
	
		$remember = false;
		
		if(Input::has('remember_token')){
			$remember = true;
		}
		
		if(Auth::attempt(['email' => Input::get('email'), 'password' => Input::get('password')], $remember)){
			 return Redirect::action('TravelersController@all');
		}else{
			 return Redirect::action('UsersController@login')
                ->with('flash_error', 1)
                ->withInput();
		}
	
	}
	
	public function logout()
	{
		Auth::logout();
		
		return Redirect::action('UsersController@login');
	}
	
	public function forbidden()
	{
		return View::make('user/forbidden');
	}
	
}
